package com.wang.project.remote.gatewayRemote;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wang.openApiCommon.model.entity.UserModel;
import com.wang.openApiCommon.remote.InnerUserService;
import com.wang.project.common.ErrorCode;
import com.wang.project.exception.BusinessException;
import com.wang.project.mapper.UserMapper;
import com.wang.project.model.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service("InnerUserService")
public class InnerUserServiceImpl implements InnerUserService {

    @Resource
    private UserMapper userMapper;

    /**
     * 给网关的远程调用服务，根据ak去查询用户
     *
     * @param accessKey
     * @return36
     */
    @Override
    public UserModel getInvokeUser(String accessKey) {
        log.info("getInvokeUser accessKey ", accessKey);
        if (StringUtils.isBlank(accessKey)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("accessKey", accessKey);
        User user = userMapper.selectOne(queryWrapper);
        UserModel userModel = new UserModel();
        BeanUtils.copyProperties(user, userModel);
        return userModel;
    }
}
